//
//  ViewController.swift
//  Core Motion
//
//  Created by 董文博 on 15/2/10.
//
//

import UIKit
import CoreMotion

class ViewController: UIViewController {
    //CMMotionManager成员变量
    var mgr:CMMotionManager!
    override func viewDidLoad() {
        super.viewDidLoad()
        //创建CMMotionManager对象
        self.mgr = CMMotionManager()
        //判断是否可用
        if(mgr.accelerometerAvailable){
            //设置采样率
            mgr.accelerometerUpdateInterval = 1.0/60.0
            //开始采样
            self.pull()
        }
    }
    //push方式
    func push(){
        self.mgr.startAccelerometerUpdatesToQueue(NSOperationQueue.mainQueue(), withHandler: { (data:CMAccelerometerData!,error:NSError!) -> Void in
            //更新UI放主队列
            //耗时操作放其他队列
            let acc:CMAcceleration = data.acceleration
            println(acc.x,acc.y,acc.z)
        })
    }
    
    //pull方式
    func pull(){
        mgr.startAccelerometerUpdates()
    }
    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        let acc:CMAcceleration = mgr.accelerometerData.acceleration
        println(acc.x,acc.y,acc.z)
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

